package com.raedev.forms.items

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.util.Log
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import com.raedev.forms.FormType
import com.raedev.forms.R
import com.raedev.forms.listener.FormViewHolder

/**
 * 日期选择表单
 * @author RAE
 * @date 2022/09/14
 * @copyright Copyright (c) https://github.com/raedev All rights reserved.
 */
@Suppress("MemberVisibilityCanBePrivate")
@SuppressLint("SimpleDateFormat")
class IdCardFormItem(
    label: String, name: String, value: String?,
    required: Boolean = false,
    dateFormat: String? = null
) : FormItem(label, name, value) {

    companion object {
        const val STYLE_ANDROID: Int = 0
        const val STYLE_SHELL: Int = 1
    }

    override val layoutId: Int = R.layout.form_item_id_card
    override val formType: Int = FormType.Button.ordinal


    override var hint: String? = null
        get() = if (field.isNullOrEmpty()) "请选择" else field


    interface OnIdAddListener {
        fun onAdd()
    }

    var addListener: OnIdAddListener? = null


    // 新增回调接口
    interface OnIdCardListener {
        fun onImageView(
            imageViewFront: AppCompatImageView,
            imageViewBack: AppCompatImageView,
            stateView: AppCompatImageView,
            tipView: AppCompatTextView
        )
    }

    var idCardListener: OnIdCardListener? = null


    init {
        this.required = required
    }

    override fun onBindViewEvent(holder: FormViewHolder) {
        super.onBindViewEvent(holder)
        val imageViewFront = holder.getView<AppCompatImageView>(R.id.tv_id_card_front)
        val imageViewBack = holder.getView<AppCompatImageView>(R.id.tv_id_card_back)
        if (name == "dimerize") {
            imageViewBack.visibility = View.VISIBLE
            imageViewFront.visibility = View.INVISIBLE
        } else {
            imageViewBack.visibility = View.INVISIBLE
            imageViewFront.visibility = View.VISIBLE
        }
        val stateView = holder.getView<AppCompatImageView>(R.id.iv_id_card_upload_state)
        val tipView = holder.getView<AppCompatTextView>(R.id.tv_card_front_tip)
        holder.itemView.setOnClickListener(this::onItemClick)

        idCardListener?.onImageView(imageViewFront,imageViewBack, stateView, tipView)
    }

    override fun onUnBindViewEvent(holder: FormViewHolder) {
        holder.itemView.setOnClickListener(null)
    }

    override fun onBindViewHolder(holder: FormViewHolder) {
        holder.updateTitleLayout()
//        holder.setHint(R.id.tv_date, this.hint, this.viewonly)
//        holder.setText(R.id.tv_date, this.value)
    }


    override fun getTitleLayoutWidth(holder: FormViewHolder): Int {
        return holder.findTitleLayout().measuredWidth
    }

    override fun onLayoutChildren(holder: FormViewHolder) {
        holder.updateTitleLayout()
    }


    /**
     * 点击事件处理
     */
    private fun onItemClick(view: View) {
        showDatePick()
    }

    /**
     * 显示Android的日期选择对话框
     */
    private fun showDatePick() {
        addListener?.onAdd()
    }


}